package com.diamond.facade;

import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;

import com.diamond.bo.FormulaBO;
import com.diamond.entity.Formula;
import com.diamond.service.IFormulaService;
import com.diamond.util.DateUtils;
import com.diamond.util.StringUtil;

public class FormulaFacade extends CommonFacade<FormulaBO> {
	
	public IFormulaService formulaService;
	
	public void setFormulaService(IFormulaService formulaService) {
		this.formulaService = formulaService;
	}

	public List findByUserID(Long id,Integer type) {
		String HQL="select * from formula as b where b.user_id="+id;
		if(type!=null)HQL+=" and state="+type;
		HQL+=" order by importance";
		try {
			List<FormulaBO> list=service.findBySqlToObj(HQL,FormulaBO.class);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 查找经销商的所有过滤公式，包括供应商公式，经销商公式，特价公式
	 * @param id
	 * @return
	 */
	public List findAllByUserID(Long id) {
		String HQL="select * from formula as b where b.state=1 or (b.state=2 and b.user_id="+id+") " +
				"order by importance,utime";
		try {
			List<FormulaBO> list=service.findBySqlToObj(HQL,FormulaBO.class);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public Integer save(FormulaBO bo,Map session){
		try {
			Object obj=session.get("USER_ID");
			if(obj==null)return -1;
			Long id=(Long) obj;
			String certStr=bo.getCerificate();
			if(!StringUtil.isEmpty(certStr)){
				bo.setCerificate(URLDecoder.decode(certStr,"UTF-8"));
			}
			bo.setUserId(id);
			bo.setUtime(DateUtils.getStringDate());
			Formula entity = new Formula();
			PropertyUtils.copyProperties(entity, bo);
			if(bo.getFormulaId()!=null){
				service.update(entity);
			}else{
				if(bo.getState()!=null&&bo.getImportance()!=null){
					String HQL="from Formula where state=? and importance=? and userId=?";
					Long userId=(Long) session.get("USER_ID");
					List list = service.find(HQL, new Object[]{bo.getState(),bo.getImportance(),userId});
					if(list!=null&&list.size()>0)return -2;
				}
				service.save(entity);
			}
			return 0;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
		
	}
	public void addOrUpdateFormula(FormulaBO formulaBo) {
		
	}
	public Integer countFormulaBo(Long id, Integer start, Integer pageSize) {
		try {
			return formulaService.countFormulaBo(id, start, pageSize);
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
	/**
	 * 查看管理员公式
	 * @return
	 */
	public List<Formula> findSupplyFormula() {
		List list=formulaService.findByHQL("from Formula where state=1 order by importance");
		return list;
	}
	/**
	 * 查看代理商公式
	 * @return
	 */
	public List<Formula> findAngentFormula(Long id) {
		List list=formulaService.findByHQL("from Formula where userId="+id+" order by importance");
		return list;
	}
	/**
	 * 查看特价公式
	 * @return
	 */
	public List<Formula> findSpeFormula(Long id) {
		List list=formulaService.findByHQL("from Formula where state=3 and userId="+id+" order by importance");
		return list;
	}
	/**
	 * 查看零售公式
	 * @return
	 */
	public List<Formula> findFormulas(Long id) {
		List list=formulaService.findByHQL("from Formula where state=2 and userId="+id+" order by importance");
		return list;
	}

	public FormulaBO findMessageByMessageId(String formulaId) throws Exception {
		return formulaService.findMessageByMessageId(formulaId);
	}

}
